package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.ReportReqPara;
import com.lst.model.Report;
import com.lst.service.ReportService;
import com.lst.utils.DateUtil;
import com.lst.utils.RequestUtils;


/**
 * @ClassName: ReportAddServlet
 * @Description: 举报添加接口
 * 
 */
public class ReportAddServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private ReportService reportService = ctx.getBean(ReportService.class);
	
	
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();
		logger.info("doPost begin: " + startDate);
		BaseResponse resPara = new BaseResponse();

		try {
			ReportReqPara reqPara = getReqPara(request);
		
		if(isCheckField(reqPara,resPara)){
			Integer userid = Integer.valueOf(reqPara.getUserid());
			Integer reportType = Integer.valueOf(reqPara.getReportType());
			String reportContent = reqPara.getReportContent();
			Integer type = Integer.valueOf(reqPara.getType());
			Integer sourceId = Integer.valueOf(reqPara.getSourceId());
			
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("createUse", userid);
			map.put("type", type);
			map.put("sourceId", sourceId);
			Report repoet2=reportService.selectByMap(map);
			if(repoet2!=null){
				resPara.setCode(CommCode.M_A000000);
				resPara.setMessage("已经举报过了");
			}else{
				String reportType2="";
				if(reportType==1){
					reportType2="存在侵权行为";
				}else if(reportType==2){
					reportType2="存在欺诈骗钱等违法行为";
				}else if(reportType==3){
					reportType2="存在辱骂、歧视、挑衅等不友善行为";
				}else{
					reportType2="发布不实消息";
				}
				
			    Report report=new Report();
			    report.setReportType(reportType2);
			    report.setReportContent(reportContent);
			    report.setStatus(false);
			    report.setCreateDate(startDate);
			    report.setCreateUser(userid);
			    report.setType(type);
			    report.setSourceId(sourceId);
			    
			    reportService.insert(report);
			
				resPara.setCode(CommCode.M_Y000000);
				resPara.setMessage(CommCode.M_Y000001);
			}
			
		  }
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}
		
		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
			logger.info("ClientDiscussServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

			logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public ReportReqPara getReqPara(HttpServletRequest request) {

		ReportReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new ReportReqPara());

			reqPara = (ReportReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
			String reportType = request.getParameter("reportType");
			String reportContent = request.getParameter("reportContent");
			String type=request.getParameter("type");
			String sourceId=request.getParameter("sourceId");
	
			logger.info("reqPara userid:" + userid);
			logger.info("reqPara reportType: " + reportType);
			logger.info("reqPara reportContent: " + reportContent);
			logger.info("reqPara type: " + type);
			logger.info("reqPara sourceId: " + sourceId);

	        reqPara.setUserid(userid);
	        reqPara.setReportType(reportType);
	        reqPara.setReportContent(reportContent);
	        reqPara.setType(type);
	        reqPara.setSourceId(sourceId);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		ReportReqPara cdReqPara = (ReportReqPara) reqPara;
		BaseResponse cdResPara = (BaseResponse) resPara;

	 if (StringUtils.isEmpty(cdReqPara.getReportType())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("举报类型为空");
		}else if (StringUtils.isEmpty(cdReqPara.getType())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("举报对象类型为空");
		}else if (StringUtils.isEmpty(cdReqPara.getSourceId())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("举报对象id为空");
		} else if (StringUtils.isEmpty(cdReqPara.getUserid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000002);
		} else {
			flag = true;
		}

		return  flag;
	}

}
